<template>
    <div class="login-container">
      <el-card class="login-card">
        <h2 class="login-title">用户登录</h2>
        <el-form :model="form" status-icon :rules="rules" ref="form" label-width="80px" class="demo-ruleForm">
          <el-form-item prop="username" label="用户名">
            <el-input v-model="form.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item prop="password" label="密码">
            <el-input
              v-model="form.password"
              type="password"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('form')">登录</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </template>
  
  <script setup>
  import { ref } from 'vue';
  
  // 表单数据
  const form = ref({
    username: '',
    password: ''
  });
  
  // 表单验证规则
  const rules = ref({
    username: [
      { required: true, message: '请输入用户名', trigger: 'blur' },
      { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      { min: 6, max: 30, message: '长度在 6 到 30 个字符', trigger: 'blur' }
    ]
  });
  
  // 提交表单方法
  const submitForm = (formName) => {
    const formEl = document.querySelector(`#${formName}`);
    if (formEl) {
      formEl.validate((valid) => {
        if (valid) {
          // 这里可以添加实际的登录逻辑，比如调用 API 进行身份验证
          console.log('提交成功', form.value);
        } else {
          console.log('表单验证失败，请检查输入');
          return false;
        }
      });
    }
  };
  </script>
  
  <style scoped>
  .login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background-color: #f5f5f5;
  }
  
  .login-card {
    width: 350px;
    padding: 20px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    background-color: #fff;
  }
  
  .login-title {
    text-align: center;
    margin-bottom: 20px;
    font-size: 24px;
    color: #333;
  }
  </style>